$(document).ready(function(){
    if($.cookie('Authorization')===null || localStorage.getItem('realname') === null) {
        showErr("您处于未登录状态，请先登录", '1000000');
        return
    }
    $('#realname').text(localStorage.getItem('realname') )

    if (getQueryVariable('id')) {
        loadInfo(getQueryVariable('id'))
    } else {
        showErr("访问路径错误")
    }

    $('.search-btn').click(function(){
        if ($('#detail-search').val() === '') {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10
            }))
        } else {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10,
                entName: $('#detail-search').val()
            }))
        }
        window.location.href = host_url + "search.html"
    })

    $('.bar-main-left').click(function(){
        localStorage.setItem('searchParam', JSON.stringify({
            pageNo: 1,
            pageSize:10
        }))
        window.location.href = host_url + "theme.html";
    })
    $('.bar-main-right').click(function(){
        window.location.href = host_url + "partner.html";
    })

    // 输入框响应回车
    $('#detail-search').keyup(function(event){
        if(event.keyCode ==13){
            $('.search-btn').click()
        }
    })

    $('.detail-main').on('click', '.detail-data-tab>span', function(){
        if (!$(this).hasClass('active')) {
            $('.detail-data-tab>span').removeClass('active')
            $(this).addClass('active')
            var tempText = $(this).text()
            if (tempText === '图表展示') {
                $('.detail-map-box,.detail-info-table1,.detail-info1').show()
                $('.detail-info2').hide()
            } else if (tempText === '公司详单') {
                $('.detail-map-box,.detail-info-table1,.detail-info1').hide()
                $('.detail-info2').show()
            }
        }
    })

    // 关注企业
    $('.detail-head').on('click', '.collect-x,.collect-y', function() {
        var id = $(this).attr('add-id')
        var that = this
        if (parseInt(id) > 0) {
            $.ajax({
                // url: service_url + '/a/personal/attentEnterprise/attent?eid='+id +'&token=' + $.cookie('Authorization'),
                url: service_url + '/a/zhiku/attentZkEntInfo/attent?eid='+id +'&token=' + $.cookie('Authorization'),
                type: 'GET',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                contentType: 'application/json',
                dataType: 'jsonp',
                jsonpCallback:"success_jsonpCallback",  
                processData: false, // 告诉jQuery不要去处理发送的数据
                success: function(data) {
                    if (data.status === '00') {
                        var msg = data.msg
                        if (msg === '关注成功！') {
                            $(that).removeClass('collect-y')
                            $(that).addClass('collect-x')
                        } else if (msg === '取消关注成功！') {
                            $(that).removeClass('collect-x')
                            $(that).addClass('collect-y')
                        }
                        // top.alert(msg,'','',{type: 'success'})

                    } else {
                        showErr(data.msg)
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest, textStatus, errorThrown)
                }
            })
        }
    })
})

function loadInfo(id) {
    $.ajax({
        url: service_url + '/a/zhiku/zkEntInfo/EntInfoDetail?id='+id+'&token=' + $.cookie('Authorization'),
        type: 'GET',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        contentType: 'application/json',
        dataType: 'jsonp',
        jsonpCallback:"success_jsonpCallback",  
        processData: false, // 告诉jQuery不要去处理发送的数据
        success: function(data) {
            if (data.status === "1000000" || data.status ==="01") {
                showErr("您处于未登录状态，请先登录", '1000000')
                return
            }

            // console.log(data)
            var data = JSON.parse(data.data)
            if (JSON.stringify(data) == "{}") {
                showErr("企业不存在")
                return
            }
            
            // 生成企业简介和企业联系方式
            var tempHtml = ''
            var esdate = data.info.esdateStr || '未知'
            var regcap = '未知'
            if (data.info.regcap !== undefined && data.info.regcap !== '') {
                regcap = data.info.regcap / 10000 >= 1 ? (data.info.regcap / 10000) + '亿元' : data.info.regcap + '万元'
            }
            var website = data.info.website || '暂无'
            var description = data.info.description || '暂无'
            var attentHtml = ''
            if (data.info.isAttent) {
                attentHtml = '<span class="collect-x" add-id="'+data.info.id+'"></span>'
            } else {
                attentHtml = '<span class="collect-y" add-id="'+data.info.id+'"></span>'
            }
            
            tempHtml = '<div class="detail-head-left"><h1>'+data.info.entName+'<div class="more">'+attentHtml+'<p class="show-more" add-data="">查看更多...</p></div></h1><div class="detail-span-box"><span>成立时间：'+esdate+' </span><span>注册资本：'+regcap+'</span><span class="link-span">官网：'+website+'</span></div><h2>公司简介：</h2><p>'+description+'</p></div><div class="detail-head-right"><h2><i></i>联系方式：</h2>'
            if (data.contact.length === 0) {
                tempHtml = tempHtml + '<p>暂无</p>'
            } else {
                for (var i = 0;i < data.contact.length; i ++) {
                    var position = data.contact[i].position || ''
                    var name = data.contact[i].name || ''
                    var tel = data.contact[i].tel || ''
                    if (position !== ''){
                        if (name !== ''){
                            tempHtml = tempHtml + '<p>'+ position+'：'+name+'    '+tel+'</p>'
                        } else {
                            tempHtml = tempHtml + '<p>'+ position+'：'+tel+'</p>'
                        }
                    } else {
                        if (name !== '') {
                            tempHtml = tempHtml + '<p>'+name+'    '+tel+'</p>'
                        }
                    }
                }
            }
            tempHtml = tempHtml + '</div>'
            $('.detail-head').append(tempHtml)
            $('.detail-head .loading_div').hide()


            // 生成企业画像
            var mainHtml = '<div class="detail-main-left">'

            var fielHtml = '<h2 class="detail-title-h2">企业画像：</h2><div class="detail-tag-box">'
            var fieldNum = 0
            if (data.info.field !== undefined) {
                var field = data.info.field
                var fieldArr = field.split(',')
                for (var i = 0; i < fieldArr.length; i++) {
                    if (fieldArr[i] !== '') {
                        fieldNum++
                        fielHtml = fielHtml + '<span>'+fieldArr[i]+'</span>'
                    }
                }
            }

            if (data.info.labelNationalHt === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>国高新</span>'
            } 
            if (data.info.labelZgcHt === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>中关村高新</span>'
            }
            if (data.info.labelSbq === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>十百千企业</span>'
            }
            if (data.info.labelDl === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>瞪羚企业</span>'
            }
            if (data.info.labelUnicorn === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>独角兽企业</span>'
            }
            if (data.info.labelTop100 === 1) {
                fieldNum++
                fielHtml = fielHtml + '<span>全国百强</span>'
            }

            if (fieldNum > 0) {
                fielHtml = fielHtml + '</div>'
            } else {
                fielHtml = ''
            }

            
            //请求分公司信息
            $.ajax({
                url: service_url + '/a/enterprise/httpclient/getInfo',
                type: 'GET',
                data: {
                    "url": "http://open.yscredit.com/api/request",
                    "api": "A008",
                    // "args": "{\"entInfo\":\"" + '北京京东世纪贸易有限公司' + "\"}"
                    "args": "{\"entInfo\":\"" + data.info.entName + "\"}"
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                dataType: 'json',
                async: true,
                headers: {
                    "Authorization": $.cookie('Authorization')
                },
                jsonpCallback:"success_jsonpCallback",  
                success: function(data1) {
                    if (data1.data === null) {
                        data1.data = {}
                        data1.data.filiationList = []
                    }
                    // 获取每个省份的分公司和投资详情
                    var provinceArr = []
                    for (var y = 0; y < areaData.length; y++) {
                        provinceArr.push({
                            name: areaData[y].name,
                            fnum: 0, // 分公司总数
                            tnum: 0, // 投资公司总数
                            duziNum: 0, // 独资公司总数
                            kongguNum: 0, // 控股公司总数
                            canguNum: 0 // 参股公司总数
                        })
                    }

                    var filiationList = data1.data.filiationList
                    for (var i = 0; i < filiationList.length; i++) {
                        // console.log(filiationList[i].brAddr +'----'+getProvinceByAddr(filiationList[i].brAddr))
                        var provinceName = getProvinceByAddr(filiationList[i].brAddr)
                        if (provinceName !== '其他') {
                            for (var j = 0; j < provinceArr.length; j++) {
                                if (provinceArr[j].name === provinceName) {
                                    provinceArr[j].fnum++
                                    break
                                }
                            }
                        }
                    }
                    
                    // 请求投资详情
                    $.ajax({
                        url: service_url + '/a/enterprise/httpclient/getInfo',
                        type: 'GET',
                        data: {
                            "url": "http://open.yscredit.com/api/request",
                            "api": "100157",
                            "args": "{\"entInfo\":\"" + data.info.entName + "\"}"
                        },
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        dataType: 'json',
                        async: true,
                        headers: {
                            "Authorization": $.cookie('Authorization')
                        },
                        jsonpCallback:"success_jsonpCallback",  
                        success: function(data2) {
                            if (data2.code === '0001' || data2.data === null) {
                                data2.data = {}
                                data2.data.content = {}
                                data2.data.content.entinvItemList = []
                            }
                            var entinvItemList = data2.data.content.entinvItemList
                            for (var i = 0; i < entinvItemList.length; i++) {
                                var provinceName = getProvinceByAddr(entinvItemList[i].regOrg)
                                // console.log(entinvItemList[i].regOrg +'----'+provinceName)
                                if (provinceName !== '其他') {
                                    for (var j = 0; j < provinceArr.length; j++) {
                                        if (provinceArr[j].name === provinceName) {
                                            provinceArr[j].tnum++
                                            var fundedRatio = entinvItemList[i].fundedRatio
                                            if (fundedRatio === '') fundedRatio = '0'
                                            if( parseInt(fundedRatio) == parseInt('100%')) {//独资
                                                provinceArr[j].duziNum++
                                            } else if(parseInt(fundedRatio) > parseInt('50%') && parseInt(fundedRatio) < parseInt('100%')) {//控股
                                                provinceArr[j].kongguNum++
                                            }else if(parseInt(fundedRatio) < parseInt('50%')){ //参股
                                                provinceArr[j].canguNum++
                                            }
                                            break
                                        }
                                    }
                                }
                            }

                            var fnumMAX = provinceArr.sort(compare1)[0]
                            var tnumMax = provinceArr.sort(compare2)[0]

                            var ftnumMax = provinceArr.sort(compare3)[0]

                            // mainHtml = mainHtml + fielHtml + '<h2 class="detail-title-h2">业务布局：<div class="detail-data-tab"><span class="active">地图展示</span>|<span>列表展示</span></div></h2><div class="detail-map-box" id="map-box"></div><div class="detail-info">'
                            mainHtml = mainHtml + fielHtml + '<h2 class="detail-title-h2">业务布局：<div class="detail-data-tab"><span class="active">图表展示</span>|<span>公司详单</span></div></h2>'

                            mainHtml = mainHtml + '<div class="detail-info" style="margin-bottom:-40px"><p>截止目前，<span class="color1">'+data.info.entName+'</span>在全国共设立了<span class="color1">'+filiationList.length+'</span>家分公司，共投资了<span class="color1">'+entinvItemList.length+'</span>家企业。'
                            if (!(fnumMAX.fnum === 0 && tnumMax.tnum === 0)) {
                                if (fnumMAX.fnum !== 0 && tnumMax.tnum === 0) {
                                    mainHtml = mainHtml + '其中，在<span class="color1">'+fnumMAX.name+'</span>设立分公司最多。'
                                } else if (fnumMAX.fnum === 0 && tnumMax.tnum !== 0) {
                                    mainHtml = mainHtml + '其中，在<span class="color1">'+tnumMax.name+'</span>投资企业最多。'
                                } else {
                                    mainHtml = mainHtml + '其中，在<span class="color1">'+fnumMAX.name+'</span>设立分公司最多,在<span class="color1">'+tnumMax.name+'</span>投资企业最多。'
                                }
                            }
                            mainHtml = mainHtml + '</p></div><div class="detail-data-main"><div class="map-main"><div class="detail-map-box" id="map-box"></div></div>'

                            mainHtml = mainHtml + '<table class="table table-striped table-bordered table-hover  detail-info-table1"><tr><th rowspan="2" style="width: 16.66%;vertical-align: middle;">区域</th><th rowspan="2" style="width: 16.66%;vertical-align: middle;">设立分公司(家)</th><th colspan="4">投资企业(家)</th></tr><tr><th style="width: 16.66%;">独资公司</th style="width: 16.66%;"><th style="width: 16.66%;">控股公司</th style="width: 16.66%;"><th style="width: 16.66%;">参股公司</th style="width: 16.66%;"><th style="width: 16.66%;">小计</th style="width: 16.66%;"></tr>'
                            if (ftnumMax.fnum + ftnumMax.tnum > 0) {
                                var fSum = 0
                                var duziSum = 0
                                var kongguSum = 0
                                var canguSum = 0
                                var allSum = 0
                                for (var m = 0; m < provinceArr.length; m++) {
                                    if (provinceArr[m].fnum !== 0 || provinceArr[m].tnum !== 0) {
                                        fSum = fSum + provinceArr[m].fnum
                                        duziSum = duziSum + provinceArr[m].duziNum
                                        kongguSum = kongguSum + provinceArr[m].kongguNum
                                        canguSum = canguSum + provinceArr[m].canguNum
                                        allSum = allSum + provinceArr[m].fnum + provinceArr[m].tnum
                                        mainHtml = mainHtml + '<tr><td>'+provinceArr[m].name+'</td><td>'+provinceArr[m].fnum+'</td><td>'+provinceArr[m].duziNum+'</td><td>'+provinceArr[m].kongguNum+'</td><td>'+provinceArr[m].canguNum+'</td><td>'+(provinceArr[m].fnum + provinceArr[m].tnum)+'</td></tr>'
                                    }
                                }
                                mainHtml = mainHtml + '<tr><td>总计</td><td>'+fSum+'</td><td>'+duziSum+'</td><td>'+kongguSum+'</td><td>'+canguSum+'</td><td>'+allSum+'</td></tr></table>'
                            } else {
                                mainHtml = mainHtml + '<tr><td>总计</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr></table>'
                            }
                            
                            mainHtml = mainHtml + '</div><div class="detail-info1">'

                            
                            
                             // 生成合作需求
                             var cooperate = data.cooperate
                             if (cooperate.length > 0) {
                                 mainHtml = mainHtml + '<h2 class="detail-left--h2">合作需求:</h2><table class="table table-striped table-bordered table-hover detail-info-table2"><tr><th style="width: 17%;">录入时间</th><th style="width: 68%;">需求描述</th><th style="width: 15%;">录入人</th></tr>'
                                 for (var p = 0; p < cooperate.length; p++) {
                                     if (cooperate[p].description !== undefined) {
                                         var createDate = cooperate[p].createDate || '近期'
                                         if (createDate != '近期') {
                                            createDate = createDate.split(' ')[0]+'<br>'+createDate.split(' ')[1]
                                         }
                                         var createName = cooperate[p].createName || '管理员'
                                         mainHtml = mainHtml + '<tr><td>'+createDate+'</td><td style="text-align:left">'+cooperate[p].description+'</td><td>'+createName+'</td></tr>'
                                     }
                                 }
                                 mainHtml = mainHtml + '</table>'
                             }
                             mainHtml = mainHtml + '</div>'

                            // 分公司列表
                            mainHtml = mainHtml + '<div class="detail-info2" style="margin-top: 50px;">'
                            if (filiationList.length > 0) {
                                mainHtml = mainHtml + '<p><b>分公司</b></p>'
                                for (var o = 0; o < filiationList.length; o++) {
                                    mainHtml = mainHtml + '<p>'+filiationList[o].brName+'</p>'
                                }
                            }
                            
                            // 投资公司列表
                            if (entinvItemList.length > 0) {
                                mainHtml = mainHtml + '<p><b>投资企业</b></p>'
                                for (var o = 0; o < entinvItemList.length; o++) {
                                    mainHtml = mainHtml + '<p>'+entinvItemList[o].entName+'</p>'
                                }
                            }
                            mainHtml = mainHtml + '</div>'
                            
                            mainHtml = mainHtml + '</div>'

                            // 生成链接互动
                            mainHtml = mainHtml + '<div class="detail-main-right"><h2 class="detail-title-h2">链接互动：</h2><ul>'

                            if (data.interaction.length === 0) {
                                mainHtml = mainHtml + '<p>暂无</p></ul></div>'
                            } else {
                                for (var m = 0; m < data.interaction.length; m++) {
                                    // 只取前8条
                                    if (data.interaction[m].description!==undefined && m < 8) {
                                        mainHtml = mainHtml + '<li><p>'+data.interaction[m].description+'</p>'
                                        if (data.interaction[m].pictures !== undefined && data.interaction[m].pictures.length > 0) {
                                            mainHtml = mainHtml + '<div class="img-box">'
                                            var tempNum = 0
                                            for (var n = 0; n < data.interaction[m].pictures.length; n++) {
                                                // 只取前两张图片
                                                if (data.interaction[m].pictures[n].id !== 0 && tempNum < 2) {
                                                    tempNum++
                                                    mainHtml = mainHtml + '<img src="'+service_url+'/a/zhiku/zkLinkInteraction/getPic?id='+data.interaction[m].pictures[n].id+'">'
                                                }
                                            }
                                            mainHtml = mainHtml + '</div>'
                                        }
                                        mainHtml = mainHtml + '</li>'
                                    }
                                }
                                mainHtml = mainHtml + '</ul></div>'
                            }
                            $('.detail-main').append(mainHtml)
                            $('.detail-main .loading_div').hide()

                            // 免登录链接生成
                            $.ajax({
                                url: service_url + '/a/zhiku/zkEntInfo/getEid?entName='+encodeURIComponent(data.info.entName)+'&token=' + $.cookie('Authorization'),
                                type: 'GET',
                                xhrFields: {
                                    withCredentials: true
                                },
                                crossDomain: true,
                                contentType: 'application/json',
                                dataType: 'jsonp',
                                jsonpCallback:"success_jsonpCallback",  
                                processData: false, // 告诉jQuery不要去处理发送的数据
                                success: function(data) {
                                    if (data.status === '00') {
                                        var item = JSON.parse(data.data)
                                        if (item.eid !== '') {
                                            $('.show-more').attr('href', 'http://zhizhao.bjzcdi.com:180/modal/wit/bus_detail.html?eid='+item.eid + '&token=' + $.cookie('Authorization'))
                                            $('.show-more').attr('add-data', item.eid)
                                        }
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    console.log(XMLHttpRequest, textStatus, errorThrown)
                                }
                            })

                            $('.show-more').click(function(){
                                var eid = $(this).attr('add-data')
                                if (eid != '') {
                                    window.open('http://zhizhao.bjzcdi.com:180/modal/wit/bus_detail.html?eid='+eid + '&token=' + $.cookie('Authorization'))
                                } else {
                                    showErr("招商平台中无匹配企业")
                                }
                            })

                            // 渲染地图
                            // console.log(data.info.regorgProvince, provinceArr)
                            var geoCoordMap = {
                                '台湾': [120.809062,23.644332],
                                '河北': [115.502461,38.045474],
                                '山西': [112.149248,37.857014],
                                '内蒙古': [111.670801,41.818311],
                                '辽宁': [123.429096,41.196767],
                                '吉林': [125.3245,43.886841],
                                '黑龙江': [127.642464,46.756967],
                                '江苏': [119.567413,33.341544],
                                '浙江': [120.153576,28.987459],
                                '安徽': [117.283042,31.86119],
                                '福建': [118.306239,26.075302],
                                '江西': [115.592151,27.676493],
                                '山东': [118.000923,36.175807],
                                '河南': [113.665412,33.757975],
                                '湖北': [112.298572,31.084355],
                                '湖南': [111.582279,27.49409],
                                '广东': [113.280637,22.725178],
                                '广西': [109.120004,23.52402],
                                '海南': [110.03119,19.031971],
                                '四川': [103.065735,30.659462],
                                '贵州': [106.713478,26.578343],
                                '云南': [101.512251,24.040609],
                                '西藏': [88.132212,31.660361],
                                '陕西': [108.948024,34.263161],
                                '甘肃': [104.293557,35.058039],
                                '青海': [95.778916,35.623178],
                                '宁夏': [106.078179,37.26637],
                                '新疆': [85.617733,40.792818],
                                '北京': [116.405285,39.904989],
                                '天津': [117.190182,39.125596],
                                '上海': [121.472644,31.231706],
                                '重庆': [107.204962,29.533155],
                                '香港': [114.173355,22.320048],
                                '澳门': [113.54909,22.198951]
                            };
                            var colorarrays = ["#238b40","#373d9b","#5a2885","#e6bf0c","#e7404a"];
                            var test =  echarts.init(document.getElementById('map-box'));
                            var fromP = data.info.regorgProvince || '北京'
                            var fromCoord = [116.405285,39.904989]
                            for (var k in geoCoordMap) {
                                if (fromP.indexOf(k) > -1) {
                                    fromP = k
                                    fromCoord = geoCoordMap[k]
                                    break
                                }
                            }
                            var mapData = []
                            var mapData1 = []
                            var hasMain = false // 分公司里是否有总部所在地址
                            // console.log('provinceArr', provinceArr)
                            for (var i = 0; i < provinceArr.length; i++) {
                                if (provinceArr[i].fnum > 0) {
                                    var toP = provinceArr[i].name
                                    var toCoord = [116.405285,39.904989]
                                    for (var k in geoCoordMap) {
                                        if (toP.indexOf(k) > -1) {
                                            toP = k
                                            toCoord = geoCoordMap[k]
                                            break
                                        }
                                    }
                                    if ( fromP === toP) {
                                        hasMain = true
                                    }
                                    mapData.push({
                                        fromName: fromP,
                                        toName: toP,
                                        coords: [fromCoord, toCoord],
                                        item: provinceArr[i]
                                    })
                                    mapData1.push({
                                        name: toP,
                                        value: toCoord.concat(provinceArr[i].fnum+provinceArr[i].tnum),
                                        item: provinceArr[i]
                                    })
                                }
                            }

                            if (!hasMain) {
                                mapData1.push({
                                    name: fromP,
                                    value: fromCoord.concat(0),
                                    item: {
                                        fnum: 0,
                                        tnum: 0
                                    }
                                })
                            }

                            // console.log(mapData, mapData1)
                            var series = [];
                            for (var i = 0; i < 1; i++) {
                                series.push({
                                    type: 'lines',
                                    zlevel: 1,
                                    effect: {
                                        show: true,
                                        period: 6,
                                        trailLength: 0.3,//小尾巴长度
                                        color: '#fff',//颜色
                                        symbolSize: 3//线条粗细
                                    },
                                    lineStyle: {
                                        normal: {
                                            color: function(params){
                                                var sum = params.data.item.fnum + params.data.item.tnum
                                                if (sum <= 2) {
                                                    return colorarrays[0]
                                                } else if (sum <= 5) {
                                                    return colorarrays[1]
                                                } else if (sum <= 10) {
                                                    return colorarrays[2]
                                                } else if (sum <= 20) {
                                                    return colorarrays[3]
                                                } else {
                                                    return colorarrays[4]
                                                }
                                            },
                                            width: 0,
                                            curveness: 0.2
                                        }
                                    },
                                    data: mapData
                                },
                                {
                                    type: 'lines',
                                    zlevel: 2,
                                    // effect: {
                                    //     show: true,
                                    //     period: 16,
                                    //     trailLength: 0,
                                    //     // symbol: planePath,
                                    //     symbolSize: 2,
                                    //     shadowBlur: 10,
                                    //     color: '#000000'
                                    // },
                                    lineStyle: {
                                        normal: {
                                            color: function(params){
                                                var sum = params.data.item.fnum + params.data.item.tnum
                                                if (sum <= 2) {
                                                    return colorarrays[0]
                                                } else if (sum <= 5) {
                                                    return colorarrays[1]
                                                } else if (sum <= 10) {
                                                    return colorarrays[2]
                                                } else if (sum <= 20) {
                                                    return colorarrays[3]
                                                } else {
                                                    return colorarrays[4]
                                                }
                                            },
                                            width: 1,
                                            opacity: 0.4,
                                            curveness: 0.2
                                        }
                                    },
                                    data: mapData
                                },
                                {
                                    name: '分公司数量',
                                    type: 'effectScatter',
                                    coordinateSystem: 'geo',
                                    zlevel: 2,
                                    rippleEffect: {
                                        brushType: 'stroke'
                                    },
                                    label: {
                                        normal: {
                                            show: true,
                                            position: 'right',
                                            formatter: '{b}'
                                        }
                                    },
                                    symbolSize: function (val) {
                                        // console.log(val[2] / 8)
                                        return 10 + val[2] % 10;
                                    },
                                    itemStyle: {
                                        normal: {
                                            color: function(params){
                                                var sum = params.data.item.fnum + params.data.item.tnum
                                                if (sum <= 2) {
                                                    return colorarrays[0]
                                                } else if (sum <= 5) {
                                                    return colorarrays[1]
                                                } else if (sum <= 10) {
                                                    return colorarrays[2]
                                                } else if (sum <= 20) {
                                                    return colorarrays[3]
                                                } else {
                                                    return colorarrays[4]
                                                }
                                            },
                                        }
                                    },
                                    data:mapData1
                                    // data:BJData.map(function (dataItem) {
                                    //     console.log(dataItem[1].name + '----' + geoCoordMap1[dataItem[1].name].concat([dataItem[1].value]))
                                    //     return {
                                    //         name: dataItem[1].name,
                                    //         value: geoCoordMap1[dataItem[1].name].concat([dataItem[1].value])
                                    //     };
                                    // })
                                });
                            }
                            option = {
                                tooltip : {
                                    trigger: 'item',
                                    textStyle:{
                                        align:'left'
                                    },
                                    formatter: function(item) {
                                        if (item.seriesIndex === 2) {
                                            if (item.value[2] === 0) {
                                                return '总公司驻地：' + item.name
                                            } else {
                                                return item.name + '<br>分公司数量：' + item.value[2] + '<br>独资公司	数量：' + item.data.item.duziNum + '<br>控股公司数量：' + item.data.item.kongguNum + '<br>参股公司数量：' + item.data.item.canguNum
                                            }
                                        }
                                    }
                                },
                                visualMap: {
                                    min: 0,
                                    max: 1000,
                                    left: 'left',
                                    top: 'bottom',
                                    splitNumber: 5,
                                    pieces: [
                                        {max: 2},
                                        {min: 2, max: 5},
                                        {min: 6, max: 10},
                                        {min: 10, max: 20},
                                        {min: 20, max: 50},
                                        {min: 50},
                                    ],
                                    color: ["#238b40","#373d9b","#5a2885","#e6bf0c","#e7404a"],
                                    textStyle: {
                                        color: '#000000'
                                    }
                                },
                                geo: {
                                    map: 'china',
                                    label: {
                                        emphasis: {
                                            show: false//鼠标悬停是否显示地名
                                        }
                                    },
                                    itemStyle: {
                                        normal: {
                                            areaColor: '#d8eefd',
                                            borderColor: '#70b6f7'
                                        },
                                        emphasis: {
                                            areaColor: '#bae2fd'
                                        }
                                    }
                                },
                                series: series
                            };
                            test.setOption(option);                      
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
        
                        }
                    })

                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {

                }
            })
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest, textStatus, errorThrown)
            showErr("企业不存在")
        }
    })
}

// 根据地址分析获得所在的省份
function getProvinceByAddr(addr) {
    var province = '其他'
    for (var i = 0; i < areaData.length; i++) {
        if (addr.indexOf(areaData[i].name) > -1) {
            province = areaData[i].name
            break
        }
    }
    if (province !== '其他') return province

    for (var i = 0; i < areaData.length; i++) {
        for (var m = 0; m < areaData[i].city.length; m++) {
                var cityName = areaData[i].city[m].name
                if (cityName.indexOf('市') > -1) {
                    cityName = cityName.split('市')[0]
                } else if (cityName.indexOf('县') > -1) {
                    cityName = cityName.split('县')[0]
                } else if (cityName.indexOf('地区') > -1) {
                    cityName = cityName.split('地区')[0]
                } else if (cityName.indexOf('自治州') > -1) {
                    cityName = cityName.split('自治州')[0]
                }
                if (addr.indexOf(cityName) > -1) {
                    province = areaData[i].name
                    break
                }
        }
        if (province !== '其他') break
    }
    if (province !== '其他') return province

    for (var i = 0; i < areaData.length; i++) {
        for (var m = 0; m < areaData[i].city.length; m++) {
                for (var n = 0; n < areaData[i].city[m].area.length; n++) {
                    var cityName = areaData[i].city[m].area[n]
                    if (cityName.indexOf('市') > -1) {
                        cityName = cityName.split('市')[0]
                    } else if (cityName.indexOf('自治县') > -1) {
                        cityName = cityName.split('自治县')[0]
                    } else if (cityName.indexOf('县') > -1) {
                        cityName = cityName.split('县')[0]
                    } else if (cityName.indexOf('地区') > -1) {
                        cityName = cityName.split('地区')[0]
                    } else if (cityName.indexOf('区') > -1) {
                        cityName = cityName.split('区')[0]
                    }
                    if (addr.indexOf(cityName) > -1) {
                        province = areaData[i].name
                        break
                    }
                }
                if (province !== '其他') break
            }
            if (province !== '其他') break
    }
    return province
}

// 根据分公司数排序
function compare1(obj1, obj2) {
    var val1 = obj1.fnum;
    var val2 = obj2.fnum;
    if (val1 > val2) {
        return -1;
    } else if (val1 < val2) {
        return 1;
    } else {
        return 0;
    }            
} 

// 根据投资公司数排序
function compare2(obj1, obj2) {
    var val1 = obj1.tnum;
    var val2 = obj2.tnum;
    if (val1 > val2) {
        return -1;
    } else if (val1 < val2) {
        return 1;
    } else {
        return 0;
    }            
} 


// 根据分公司数+投资公司数排序
function compare3(obj1, obj2) {
    var val1 = obj1.fnum + obj1.tnum;
    var val2 = obj2.fnum + obj2.tnum;
    if (val1 > val2) {
        return -1;
    } else if (val1 < val2) {
        return 1;
    } else {
        return 0;
    }            
} 